package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.AppBean;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.activity.app.AppDetailActivity;
import com.zzhou.common.library.util.ToastShow;

import java.util.List;

/**
 * 上图下字的app adapter
 * Created by Mr'Tang on 2018/9/10.
 */

public class AppCommonAdapter extends android.widget.BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;
    private List<AppBean> mData;

    public AppCommonAdapter(Context context, List<AppBean> apps) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mData = apps;
    }

    public void addAll(List<AppBean> data, boolean append) {
        if (mData != null) {
            if (!append) {
                mData.clear();
            }
            mData.addAll(data);
        } else {
            mData = data;
        }
        refreshData();
    }

    public void refreshData() {
        notifyDataSetChanged();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public AppBean getItem(int position) {
        return mData.get(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int a = position + 1;
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_common_app, null);
            viewHolder = new ViewHolder();
            viewHolder.app_logo =  convertView.findViewById(R.id.app_logo);
            viewHolder.app_name =  convertView.findViewById(R.id.app_text);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final AppBean app = getItem(position);
        ImageLoader.loadAds(mContext,app.getLogo(), viewHolder.app_logo);
        viewHolder.app_name.setText(app.getName());
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                AppDetailActivity.launch(mContext,app.getId());
            }
        });
        return convertView;
    }

    class ViewHolder {
        private ImageView app_logo;
        private TextView app_name;
    }
}
